package com.t1.demo.Share;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Description:发送桌面实施截图
 * @Author: 杨淮尊
 * @Date: 2020/4/13
 **/
public class Socket_Server extends Thread {
    //初始化Socket Server Port
    public static int SERVERPORT=8888;
    //服务端Socket端口代码
    private ServerSocket serverSocket;
    //Robot自动测试
    private Robot robot;
    //获取屏幕尺寸
    public Dimension screen;
    //绘制矩形
    public Rectangle rect;
    //SocketClient
    private Socket socket;

    //构造方法  开启套接字连接
    public Socket_Server(int SERVERPORT)
    {
        try {
            //设置ServerSocket相关信息
            serverSocket = new ServerSocket(SERVERPORT);
            serverSocket.setSoTimeout(864000000);
            //实例化Robot
            robot = new Robot();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Toolkit获取屏幕共享方屏幕尺寸
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        //使用Rectangle在学员端绘制教师端桌面相同大小画布
        rect = new Rectangle(screen);
    }

    @Override
    public void run()
    {
        //实时等待接收截屏消息
        while(true)
        {
            try{
                socket = serverSocket.accept();
                //提示信息
                ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
                zip.setLevel(9);     //设置压缩级别 0-9 存储、最快、较快、标准、较好、最好
                //获取桌面的截图 不包括鼠标
                BufferedImage img = robot.createScreenCapture(rect);
                //绘制鼠标
                //获取本地图像设备
                GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                int width = gd.getDisplayMode().getWidth();
                int height = gd.getDisplayMode().getHeight();
                //使用图片代替光标
                File file = new File("C:\\Users\\2\\Desktop\\Test\\mouse.png");
                BufferedImage blackSquare = ImageIO.read(file);
                Image scaledInstance = blackSquare.getScaledInstance(15, 15, Image.SCALE_SMOOTH);
                //获取鼠标信息
                PointerInfo pointer = MouseInfo.getPointerInfo();
                int x = (int) pointer.getLocation().getX();
                int y = (int) pointer.getLocation().getY();
                img.getGraphics().drawImage(scaledInstance, x, y, null);
                //压缩
                zip.putNextEntry(new ZipEntry("test.jpg"));
                ImageIO.write(img, "jpg", zip);
                if(zip!=null)zip.close();
            } catch (IOException ioe) {
                System.out.println("连接断开");
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {e.printStackTrace();}
                }
            }
        }
    }
}